package org.weed.iremote.activity;

import org.weed.iremote.R;
import org.weed.iremote.service.RemoteServiceImpl;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class TvRemote extends Activity implements OnClickListener {
	private RemoteServiceImpl remoteService;
	private String previousChannel;
	private String currentChannel;
	private TextView textInputChannel;
	private ImageButton one;
	private ImageButton two;
	private ImageButton three;
	private ImageButton four;
	private ImageButton five;
	private ImageButton six;
	private ImageButton seven;
	private ImageButton eight;
	private ImageButton nine;
	private ImageButton zero;
	private ImageButton up;
	private ImageButton down;
	private ImageButton enter;
	private ImageButton clear;

    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        remoteService = new RemoteServiceImpl("192.168.1.107", 8888);
        setContentView(R.layout.a_tv_remote);
        textInputChannel = (TextView) this.findViewById(R.id.text_input_channel);
        one = (ImageButton) this.findViewById(R.id.dial_num_one);
        one.setOnClickListener(this);
        two = (ImageButton) this.findViewById(R.id.dial_num_two);
        two.setOnClickListener(this);
        three = (ImageButton) this.findViewById(R.id.dial_num_three);
        three.setOnClickListener(this);
        four = (ImageButton) this.findViewById(R.id.dial_num_four);
        four.setOnClickListener(this);
        five = (ImageButton) this.findViewById(R.id.dial_num_five);
        five.setOnClickListener(this);
        six = (ImageButton) this.findViewById(R.id.dial_num_six);
        six.setOnClickListener(this);
        seven = (ImageButton) this.findViewById(R.id.dial_num_seven);
        seven.setOnClickListener(this);
        eight = (ImageButton) this.findViewById(R.id.dial_num_eight);
        eight.setOnClickListener(this);
        nine = (ImageButton) this.findViewById(R.id.dial_num_nine);
        nine.setOnClickListener(this);
        zero = (ImageButton) this.findViewById(R.id.dial_num_zero);
        zero.setOnClickListener(this);
        up = (ImageButton) this.findViewById(R.id.dial_num_up);
        up.setOnClickListener(this);
        down = (ImageButton) this.findViewById(R.id.dial_num_down);
        down.setOnClickListener(this);
        enter = (ImageButton) this.findViewById(R.id.dial_btn_enter);
        enter.setOnClickListener(this);
        clear = (ImageButton) this.findViewById(R.id.dial_btn_clear);
        clear.setOnClickListener(this);
    }
    
    @Override
    public void onPause() {
    	Log.d(this.getClass().getName(), "onPause");
    	remoteService.disconnect();
    	super.onPause();    	
    }
    
	@Override
	public void onResume() {
		super.onResume();
		if (remoteService.connect()) {
			Toast.makeText(this, "connect", Toast.LENGTH_SHORT).show();
		} else {
        	Toast.makeText(this, "Unable to connect to server", Toast.LENGTH_SHORT).show();
		}
	}
    
    @Override
    public void onDestroy() {
    	Log.d(this.getClass().getName(), "onDestroy");
    	super.onDestroy();
    }
    
    @Override
    public void onBackPressed() {
    	Log.d(this.getClass().getName(), "onBackPressed");
    	super.onBackPressed();
    }

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.dial_num_zero:
			textInputChannel.append("0");
			break;
		case R.id.dial_num_one:
			textInputChannel.append("1");
			break;
		case R.id.dial_num_two:
			textInputChannel.append("2");
			break;
		case R.id.dial_num_three:
			textInputChannel.append("3");
			break;
		case R.id.dial_num_four:
			textInputChannel.append("4");
			break;
		case R.id.dial_num_five:
			textInputChannel.append("5");
			break;
		case R.id.dial_num_six:
			textInputChannel.append("6");
			break;
		case R.id.dial_num_seven:
			textInputChannel.append("7");
			break;
		case R.id.dial_num_eight:
			textInputChannel.append("8");
			break;
		case R.id.dial_num_nine:
			textInputChannel.append("9");
			break;
		case R.id.dial_num_down:
			remoteService.down();
			break;
		case R.id.dial_btn_enter:
			remoteService.channel("" + textInputChannel.getText());
			textInputChannel.setText("");
			break;
		case R.id.dial_num_up:
			remoteService.up();
			break;
		case R.id.dial_btn_clear:
			textInputChannel.setText("");
			break;
		default:
			
		}
		
	}
    
}
